﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class MediaInNewsDao : AbstractDao<MediaInNews>, IMediaInNewsDao
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Instance of MediaInNewsKey class.</param>
        /// <returns></returns>
        public override MediaInNews
        getById(object id)
        {
            try
            {
                if (!id.GetType().Equals(typeof(MediaInNewsKey)))
                {
                    //throw exception that notice about invalid type of param.
                    return null;
                }

                var k = (MediaInNewsKey)id;
                return getSingle(i => i.mId == k.MediaId && i.newsId == k.NewsId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public MediaInNews
        getById(int mId, int newsId)
        {
            return getSingle(i => i.mId == mId && i.newsId == newsId);
        }

        public List<MediaInNews>
        getByMedia(int mId)
        {
            try
            {
                return getMultiple(i => i.mId == mId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<MediaInNews>
        getByNews(int newsId)
        {
            try
            {
                return getMultiple(i => i.newsId == newsId);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IMediaInNewsDao

    public interface IMediaInNewsDao : IDao<MediaInNews>
    {
        MediaInNews getById(int medId, int newsId);

        List<MediaInNews> getByMedia(int medId);

        List<MediaInNews> getByNews(int newsId);
    }
    #endregion
}